package com.goodving.guli.activity;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.goodving.guli.R;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.C1154e;
import com.goodving.guli.bt.DimPanelFragment;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.eventbus.EventUserPicked;
import com.goodving.guli.bt.eventbus.EventWeightChanged;
import com.goodving.guli.bt.model.Birthday;
import com.goodving.guli.bt.model.WeightInfo;
import com.goodving.guli.bt.model.WeightInfoManager;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.ScaleWeightInfo;
import com.goodving.guli.bt.ui.WeightUserAvatar;
import com.goodving.guli.bt.utils.WeightUtil;
import com.goodving.guli.bt.weight.LuaUtil;
import com.goodving.guli.bt.weight.UserInfo;
import com.goodving.guli.bt.weight.UserInfoManager;
import com.goodving.guli.bt.weight.as;
import com.goodving.guli.view.C3247b;
import com.goodving.guli.view.C3657a;
import com.goodving.guli.view.C4220a;

import java.text.SimpleDateFormat;
import java.util.Date;

import de.greenrobot.event.EventBus;

/**
 * Created by john on 2015/12/9.
 */
public class WeightReportActivity extends SystemBarTintActivity implements View.OnClickListener {
    private static final String TAG = "WeightReportActivity";
    private TextView f11238A;
    private TextView f11239B;
    private TextView f11240C;
    private TextView f11241D;
    private TextView f11242E;
    private TextView f11243F;
    private String[] f11244G;
    private boolean f11245H;
    private float f11246I;
    private as f11247J;
    private boolean f11248K;
    private Context f11249b;
    private TextView f11250c;
    private TextView f11251d;
    private TextView f11252e;
    private LinearLayout f11253f;
    private RelativeLayout f11254g;
    private ImageButton f11255h;
    private RelativeLayout bmiLayout;
    private TextView f11257j;
    private boolean f11258k;
    private WeightInfo mWeightInfo;
    private UserInfo mUserInfo;
    private ScaleWeightInfo mScaleWeightInfo;
    private int f11262o;
    private int f11263p;
    private TextView f11264q;
    private TextView f11265r;
    private TextView f11266s;
    private WeightUserAvatar mWeightUserAvatar;
    private TextView f11268u;
    private ImageButton delImageButton;
    private RelativeLayout babyLayout;
    private TextView f11271x;
    private TextView f11272y;
    private TextView f11273z;

    public WeightReportActivity() {
        this.f11249b = this;
        this.f11250c = null;
        this.f11251d = null;
        this.f11252e = null;
        this.f11258k = false;
        this.delImageButton = null;
        this.f11244G = new String[0];
        this.f11245H = false;
        this.f11246I = 0.0f;
        this.f11248K = false;
    }

    private void m15653a() {
        this.delImageButton = (ImageButton) findViewById(R.id.weight_report_dele_btn);
        this.delImageButton.setOnClickListener(this);
        this.mWeightUserAvatar = (WeightUserAvatar) findViewById(R.id.user_icon);
        this.f11268u = (TextView) findViewById(R.id.user_name);
        this.f11253f = (LinearLayout) findViewById(R.id.bg_layout);
        this.f11255h = (ImageButton) findViewById(R.id.wr_share);
        this.f11254g = (RelativeLayout) findViewById(R.id.app_share_layout);
        this.f11255h.setOnClickListener(this);
        this.f11264q = (TextView) findViewById(R.id.current_weight_title);
        this.f11265r = (TextView) findViewById(R.id.current_weight_value);
        this.f11266s = (TextView) findViewById(R.id.current_weight_tips);
        this.f11250c = (TextView) findViewById(R.id.wr_bmi_tips_tv);
        this.f11251d = (TextView) findViewById(R.id.wr_figure_tips_tv);
        if (Utils.m8087m()) {
            GuliLog.tracePrintInfo(TAG, "isNoChinese");
            this.f11250c.setTextSize((float) Utils.m7961a(5.0f, this.f11249b));
            this.f11251d.setTextSize((float) Utils.m7961a(5.0f, this.f11249b));
        } else {
            GuliLog.tracePrintInfo(TAG, "isChinese");
            this.f11250c.setTextSize((float) Utils.m7961a(10.0f, this.f11249b));
            this.f11251d.setTextSize((float) Utils.m7961a(10.0f, this.f11249b));
        }
        this.f11252e = (TextView) findViewById(R.id.wr_home_back);
        this.f11252e.setOnClickListener(this);
        this.bmiLayout = (RelativeLayout) findViewById(R.id.bmi_layout);
        this.bmiLayout.setOnClickListener(this);
        this.f11257j = (TextView) findViewById(R.id.share_text);
        this.f11257j.setText(getString(R.string.app_name) + " " + new SimpleDateFormat(getString(R.string.date_month_day)).format(new Date()));
        GuliLog.tracePrintInfo("SHARE", "share time text " + this.f11257j.getText().toString());
        m15659b();
    }

    private void m15654a(int i, int i2) {
        this.f11244G = Utils.m8025a(i, i2, this.mScaleWeightInfo.m25497h());
        this.f11273z.setText(Utils.m8077i(this.f11244G[2]));
        this.f11238A.setText(Utils.m8077i(this.f11244G[3]));
        this.f11239B.setText(Utils.m8077i(this.f11244G[4]));
        this.f11240C.setText(Utils.m8077i(this.f11244G[5]));
        this.f11241D.setText(Utils.m8077i(this.f11244G[6]));
        this.f11242E.setText(Utils.m8077i(this.f11244G[7]));
        this.f11243F.setText(Utils.m8077i(this.f11244G[8]));
        this.f11271x.setText(Utils.m8057d(this.f11249b, i) + getString(R.string.weight_ref) + "(" + WeightUtil.m15698a(this.f11249b, this.mScaleWeightInfo.m25497h()) + ")");
    }

    class aq implements as.az {
        final /* synthetic */ WeightReportActivity f11353a;

        aq(WeightReportActivity weightReportActivity) {
            this.f11353a = weightReportActivity;
        }

        public void m15790a () { // m15791a
            this.f11353a.f11248K = false;
            if (C3246a.m22924b(this.f11353a.f11249b)) {
                this.f11353a.getWindow().getDecorView().destroyDrawingCache();
                return;
            }
            View findViewById = this.f11353a.findViewById(Window.ID_ANDROID_CONTENT);
            if (findViewById != null) {
                findViewById.destroyDrawingCache();
            }
        }
    }

    private void m15655a(Bundle bundle) {
        if (!this.f11248K) {
            Bitmap drawingCache;
            this.f11248K = true;
            if (this.f11247J == null) {
                this.f11247J = (as) Fragment.instantiate(this.f11249b, as.class.getName());
                this.f11247J.setArguments(bundle);
                this.f11247J.m15802a(new aq(this));
            }
            View decorView;
            if (C3246a.m22924b(this.f11249b)) {
                decorView = getWindow().getDecorView();
                decorView.buildDrawingCache();
                drawingCache = decorView.getDrawingCache();
            } else {
                decorView = findViewById(Window.ID_ANDROID_CONTENT);
                if (decorView != null) {
                    decorView.buildDrawingCache();
                    drawingCache = decorView.getDrawingCache();
                } else {
                    drawingCache = null;
                }
            }
            this.f11247J.m15801a(drawingCache);
            FragmentTransaction beginTransaction = getFragmentManager().beginTransaction();
            beginTransaction.addToBackStack(null);
            this.f11247J.show(beginTransaction, null);
        }
    }

    private void m15656a(UserInfo userInfo, boolean z) {
        if (!(userInfo == null || this.mScaleWeightInfo == null)) {
            GuliLog.tracePrintInfo(TAG, "onActivityResult notify UI  " + userInfo.toString() + " weight  " + this.mScaleWeightInfo.toString());
            GuliLog.tracePrintInfo(TAG, "timestamp " + this.mScaleWeightInfo.m25494e());
        }
        ScaleWeightInfo c3925e = new ScaleWeightInfo();
        c3925e.m25488b(this.mScaleWeightInfo.m25499j());
        c3925e.m25482a(this.f11246I);
        c3925e.setUnitType(this.mScaleWeightInfo.m25497h());
        c3925e.setDate(this.mScaleWeightInfo.m25494e());
        String a = z ? WeightUtil.m15696a(this.mScaleWeightInfo.m25494e(), 0) : WeightUtil.m15696a(this.mScaleWeightInfo.m25494e(), this.f11263p);
        WeightInfoManager a2 = WeightInfoManager.getInstance();
        WeightInfo b = a2.m15760b(c3925e.m25494e());
        if (b != null) {
            a2.m15750a(b, userInfo.uid);
        } else {
            GuliLog.tracePrintInfo(TAG, "can not go here,something wrong!!!");
        }
        LuaUtil.getInstance(this.f11249b).m15961a(this.mScaleWeightInfo, userInfo, a);
        if (z) {
            EventBus.getDefault().post(new EventWeightChanged(true, userInfo.uid));
        } else {
            EventBus.getDefault().post(new EventWeightChanged(this.f11263p, userInfo.uid));
        }
    }

    class ao implements View.OnClickListener {
        final /* synthetic */ WeightReportActivity f11350a;

        ao(WeightReportActivity weightReportActivity) {
            this.f11350a = weightReportActivity;
        }

        public void onClick(View view) {
            C0668a.m8126a(this.f11350a.f11249b, C0668a.ht);
            Intent intent = new Intent(this.f11350a.f11249b, BabyWeightInfoActivity.class);
            intent.putExtra("GENDER", this.f11350a.mUserInfo.gender);
            intent.putExtra("UNIT", this.f11350a.mScaleWeightInfo.m25497h());
            this.f11350a.startActivity(intent);
        }
    }

    private void m15659b() {
        this.babyLayout = (RelativeLayout) findViewById(R.id.baby_layout);
        this.f11271x = (TextView) findViewById(R.id.left_title);
        this.f11272y = (TextView) findViewById(R.id.right_title);
        this.f11272y.setOnClickListener(new ao(this));
        this.f11273z = (TextView) findViewById(R.id.content_min_3);
        this.f11238A = (TextView) findViewById(R.id.content_min_2);
        this.f11239B = (TextView) findViewById(R.id.content_min_1);
        this.f11240C = (TextView) findViewById(R.id.content_min_0);
        this.f11241D = (TextView) findViewById(R.id.content_add_1);
        this.f11242E = (TextView) findViewById(R.id.content_add_2);
        this.f11243F = (TextView) findViewById(R.id.content_add_3);
    }

    private void m15661c() {
        this.f11268u.setText(this.mUserInfo.name.isEmpty() ? getString(R.string.visitor) : this.mUserInfo.name);
        this.mWeightUserAvatar.m15375a(this.mUserInfo.name.isEmpty() ? getString(R.string.visitor) : this.mUserInfo.name);
        Utils.m8008a(this.mUserInfo, this.mWeightUserAvatar);
        this.f11265r.setText(WeightUtil.m15715c(this.mScaleWeightInfo.m25499j()) + C3657a.f18328f);
        this.f11264q.setText(getString(R.string.current_weight) + "(" + WeightUtil.m15698a(this.f11249b, this.mScaleWeightInfo.m25497h()) + ")");
        this.f11253f.setBackgroundResource(R.drawable.weight_report_bg);
        this.f11245H = this.mUserInfo.targetWeight > 0.0f;
        this.f11246I = WeightUtil.m15693a(this.mUserInfo.height, Birthday.fromStr(this.mUserInfo.birthday).getAge(), WeightUtil.m15689a(this.mScaleWeightInfo.m25499j(), this.mScaleWeightInfo.m25497h()));
        int ageInMonths = Birthday.fromStr(this.mUserInfo.birthday).getAgeInMonths();
        if (ageInMonths <= 83) {
            this.bmiLayout.setVisibility(View.GONE);
            this.babyLayout.setVisibility(View.VISIBLE);
            m15654a(ageInMonths, this.mUserInfo.gender);
        } else {
            this.bmiLayout.setVisibility(View.VISIBLE);
            this.babyLayout.setVisibility(View.GONE);
            if (this.mUserInfo.height >= 100) {
                this.f11250c.setText(WeightUtil.m15709b(this.f11246I));
                this.f11251d.setText(WeightUtil.m15697a(this.f11249b, this.f11246I, Birthday.fromStr(this.mUserInfo.birthday).getAge(), this.mUserInfo.gender));
            }
        }
        if (this.mUserInfo.uid == 0) {
            return;
        }
        float c;
        if (this.f11245H) {
            c = WeightUtil.m15716c(((float) ((int) WeightUtil.m15706b(this.mUserInfo.targetWeight, this.mScaleWeightInfo.m25497h()))) - this.mScaleWeightInfo.m25499j(), 1);
            if (c > 0.0f) {
                this.f11266s.setText(getString(R.string.lighter_than_goal, new Object[]{Math.abs(c) + WeightUtil.m15698a(this.f11249b, this.mScaleWeightInfo.m25497h())}));
                return;
            } else if (c < 0.0f) {
                this.f11266s.setText(getString(R.string.heavier_than_goal, new Object[]{Math.abs(c) + WeightUtil.m15698a(this.f11249b, this.mScaleWeightInfo.m25497h())}));
                return;
            } else {
                this.f11266s.setText(getString(R.string.weight_reach_goal));
                return;
            }
        }
        WeightInfo a = WeightInfoManager.getInstance().m15744a(this.mUserInfo.uid, this.mScaleWeightInfo.m25494e());
        if (a != null) {
            c = WeightUtil.m15716c(this.mScaleWeightInfo.m25499j() - WeightUtil.m15706b(a.weight, this.mScaleWeightInfo.m25497h()), 1);
            if (c > 0.0f) {
                this.f11266s.setText(getString(R.string.add_than_last_time, new Object[]{c + WeightUtil.m15698a(this.f11249b, this.mScaleWeightInfo.m25497h())}));
            } else if (c < 0.0f) {
                c = Math.abs(c);
                this.f11266s.setText(getString(R.string.less_than_last_time, new Object[]{c + WeightUtil.m15698a(this.f11249b, this.mScaleWeightInfo.m25497h())}));
            }
        }
    }

    private void m15663d() {
//        Parcelable e = m15664e();
//        Intent intent = new Intent();
//        intent.setClass(this.f11249b, ShareActivity.class);
//        intent.putExtra("share_data", e);
//        startActivity(intent);
    }

//    private ShareData m15664e() {
//        GuliLog.tracePrintInfo(TAG, "prepareShareData...");
//        ShareData shareData = new ShareData();
//        shareData.type = C3636v.f18208C;
//        shareData.fuid = this.mUserInfo.uid;
//        shareData.weightTitle = this.f11264q.getText().toString();
//        shareData.weightValue = this.f11265r.getText().toString();
//        shareData.weightTips = this.f11266s.getText().toString();
//        shareData.weightFigure = this.f11251d.getText().toString();
//        shareData.weightBMI = this.f11250c.getText().toString();
//        shareData.babyMonthsStr = this.f11271x.getText().toString();
//        String str = C3657a.f18328f;
//        GuliLog.tracePrintInfo(TAG, "mRefWeightDatas " + this.f11244G);
//        String str2 = str;
//        for (String str3 : this.f11244G) {
//            GuliLog.tracePrintInfo(TAG, "from str " + str3);
//            String str32 = Utils.m8077i(str32);
//            GuliLog.tracePrintInfo(TAG, "to str " + str32);
//            str2 = str2 + str32 + ",";
//        }
//        if (str2.isEmpty()) {
//            shareData.babyRefWeights = C3657a.f18328f;
//        } else {
//            shareData.babyRefWeights = str2.substring(0, str2.length() - 1);
//        }
//        GuliLog.tracePrintInfo("SHARE_REF_WEIGHT", "from weight report " + shareData.babyRefWeights);
//        return shareData;
//    }

    public void onBackPressed() {
        GuliLog.tracePrintInfo(TAG, "onBackPressed " + this.f11258k);
        Intent intent = new Intent();
        if (!this.f11258k) {
            intent.putExtra("RESULT_INTENT_DATA", this.f11262o);
        }
        setResult(-1, intent);
        finish();
    }

    static public class ar extends DimPanelFragment {
        protected int inflateLayout() {
            return R.layout.dialog_weight_info_modify_delete_confirm;
        }

        public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
            View onCreateView = super.onCreateView(layoutInflater, viewGroup, bundle);
            Bundle arguments = getArguments();
            if (!(arguments == null || arguments.getString("Msg") == null)) {
                ((TextView) onCreateView.findViewById(R.id.msg)).setText(arguments.getString("Msg"));
            }
            return onCreateView;
        }
    }


    class ap implements C1154e {
        final /* synthetic */ DimPanelFragment f11351a;
        final /* synthetic */ WeightReportActivity f11352b;

        ap(WeightReportActivity weightReportActivity, DimPanelFragment c1185c) {
            this.f11352b = weightReportActivity;
            this.f11351a = c1185c;
        }

        public void m10600a(DialogFragment dialogFragment) {
            dialogFragment.dismiss();
        }

        public void m10601b(DialogFragment dialogFragment) {
            this.f11351a.dismiss();
        }

        public void m10602c(DialogFragment dialogFragment) {
            this.f11351a.dismiss();
            GuliLog.tracePrintInfo(TAG, "del and del");
            GuliLog.tracePrintInfo(TAG, "weightadvdata " + this.f11352b.mScaleWeightInfo.m25494e());
            this.f11352b.setResult(-1);
            GuliLog.tracePrintInfo(TAG, "nowInfo.uid " + this.f11352b.mUserInfo.uid);
            GuliLog.tracePrintInfo(TAG, "fromUID " + this.f11352b.f11263p);
            WeightInfo b = WeightInfoManager.getInstance().m15760b(this.f11352b.mScaleWeightInfo.m25494e());
            if (b == null) {
                C3247b.m22930a(this.f11352b.f11249b, this.f11352b.getString(R.string.can_not_del_last_weight_info), 0).show();
                return;
            }
            WeightUtil.deleteWeightInfoMarkFully(b);
            EventBus.getDefault().post(new EventWeightChanged(b.uid));
            this.f11352b.finish();
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.wr_share:
                m15663d();
                C0668a.m8126a(this.f11249b, C0668a.gn);
                break;
            case R.id.wr_home_back:
                onBackPressed();
                break;
            case R.id.bmi_layout:
                Bundle bundle = new Bundle();
                bundle.putFloat(as.f11354a, this.f11246I);
                bundle.putInt(as.f11355b, Birthday.fromStr(this.mUserInfo.birthday).getAge());
                bundle.putInt(as.f11356c, this.mUserInfo.gender);
                bundle.putInt(as.f11357d, this.mUserInfo.height);
                m15655a(bundle);
                C0668a.m8126a(this.f11249b, C0668a.go);
                break;
            case R.id.weight_report_dele_btn:
                C0668a.m8126a(this.f11249b, C0668a.fA);
                String string = getString(R.string.sure_to_delete_weight);
                DimPanelFragment arVar = new ar();
                arVar.setOpClickListener(new ap(this, arVar));
                Bundle bundle2 = new Bundle();
                bundle2.putString("Msg", string);
                DimPanelFragment.showPanel((Activity) this, arVar, bundle2);
                break;
            default:
                break;
        }
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.activity_weight_report);
        m15653a();
        Intent intent = getIntent();
        if (intent != null) {
            this.mUserInfo = UserInfo.fromJsonString(intent.getStringExtra(UserInfo.USERINFO_KEY));
            this.mScaleWeightInfo = ScaleWeightInfo.m25479b(intent.getStringExtra(ScaleWeightInfo.WEIGHTADVDATA_KEY));
            if (this.mUserInfo == null) {
                GuliLog.tracePrintInfo(TAG, "info is null");
                this.mUserInfo = new UserInfo();
                this.mUserInfo.name = getString(R.string.visitor);
                this.mUserInfo.uid = 0;
                this.mUserInfo.height = C4220a.aC;
                this.mUserInfo.birthday = "2000-10";
                this.mUserInfo.targetWeight = -1;
            } else {
                this.mUserInfo.avatarSource = UserInfoManager.getInstance().getInfoByUid(this.mUserInfo.uid).avatarSource;
                GuliLog.tracePrintInfo(TAG, "info is not null");
            }
            GuliLog.tracePrintInfo(TAG, "receive userinfo " + this.mUserInfo);
            GuliLog.tracePrintInfo(TAG, "receive weightadvdata " + this.mScaleWeightInfo);
            this.f11258k = intent.getBooleanExtra("NEW_USER", false);
            this.f11263p = this.mUserInfo.uid;
            this.f11262o = this.f11263p;
        }
        this.mWeightInfo = WeightInfoManager.getInstance().m15760b(this.mScaleWeightInfo.m25494e());
        m15661c();
        if (this.f11258k && this.mUserInfo.uid != 0) {
            m15656a(this.mUserInfo, true);
        }
    }

    protected void onDestroy() {
        super.onDestroy();
    }

    public void onEvent(EventUserPicked eventUserPicked) {
        GuliLog.tracePrintInfo(TAG, "onEventUserSelect " + eventUserPicked.uid);
        this.f11262o = eventUserPicked.uid;
        this.mUserInfo = UserInfoManager.getInstance().getInfoByUid(this.f11262o);
        m15661c();
        m15656a(this.mUserInfo, false);
    }

    protected void onPause() {
        super.onPause();
        C0668a.m8141b(C0668a.aQ);
        C0668a.m8138b((Activity) this);
    }

    protected void onResume() {
        super.onResume();
        C0668a.m8133a(C0668a.aQ);
        C0668a.m8124a((Activity) this);
    }
}
